package geon.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import geon.model.Telefone;
import geon.model.TelefoneTipo;

public class DaoTelefone implements Dao<Telefone> {

	private DaoGenerico dao;
	private String sql;

	public DaoTelefone() {
		dao = new DaoGenerico();
	}

	@Override
	public Integer add(Telefone o) throws SQLException {
		try {
			dao.begin();
			sql = "Insert Into TbTelefone (TelCodigo, TelTipo, TelNumero) Values (?, ?, ?)";
			o.setCodigo(dao.getNextId("TbTelefone", "TelCodigo"));
			dao.executeCommand(sql, o.getCodigo(), o.getTipo().toString(), o.getNumero());
			dao.commit();
		} catch (Exception e) {
			Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, e);
			dao.rollback();
		}
		return o.getCodigo();
	}

	@Override
	public void upd(Telefone o) throws SQLException {
		try {
			dao.begin();
			sql = "Update TbTelefone Set TelTipo = ?, TelNumero = ? Where TelCodigo = ?";
			dao.executeCommand(sql, o.getTipo().toString(), o.getNumero(), o.getCodigo());
			dao.commit();
		} catch (Exception e) {
			Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, e);
			dao.rollback();
		}		
	}

	@Override
	public void del(int codigo) throws SQLException {
		try {
			dao.begin();
			sql = "Delete From TbTelefone Where TelCodigo = ?";
			dao.executeCommand(sql, codigo);
			dao.commit();
		} catch (Exception e) {
			Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, e);
			dao.rollback();
		}
	}

	@Override
	public Telefone set(ResultSet rs) throws SQLException {		
		Telefone telefone = new Telefone();
		telefone.setCodigo(rs.getInt("TelCodigo"));
		telefone.setTipo(TelefoneTipo.valueOf(rs.getString("TelTipo")));
		telefone.setNumero(rs.getString("TelNumero"));
		return telefone;
	}

	@Override
	public Telefone get(int codigo) throws SQLException {
		Telefone telefone = null;
		sql = "Select TelCodigo, TelTipo, TelNumero From TbTelefone Where TelCodigo = ?";
		ResultSet rs = dao.executeQuery(sql, codigo);
		if (rs.next()) {
			telefone = set(rs);
		}
		rs.close();
		return telefone;
	}
	
	public Telefone get(Object param) throws SQLException {
		Telefone telefone = null;
		sql = "Select TelCodigo, TelTipo, TelNumero From TbTelefone Where TelNumero = ?";
		ResultSet rs = dao.executeQuery(sql, param);
		if (rs.next()) {
			telefone = set(rs);
		}
		rs.close();
		return telefone;
	}

	public List<Telefone> list() throws SQLException {
		List<Telefone> telefones = new LinkedList<Telefone>();
		sql = "Select TelCodigo, TelTipo, TelNumero From TbTelefone";
		ResultSet rs = dao.executeQuery(sql);
		while (rs.next()) {
			telefones.add(set(rs));
		}
		rs.close();
		return telefones;
	}

	@Override
	public List<Telefone> list(Object... params) throws SQLException {
		List<Telefone> telefones = new LinkedList<Telefone>();
		sql = "Select TelCodigo, TelTipo, TelNumero From TbTelefone Where TelNumero Like ?";
		ResultSet rs = dao.executeQuery(sql, "%" + params[0] + "%");
		while (rs.next()) {
			telefones.add(set(rs));
		}
		rs.close();
		return telefones;
	}

}
